<?php
namespace My\Controller;

use Utuo\BackgroundController;

class AgentOrderSettleController extends BackgroundController {
    
    protected $status = array(
        '0' => "未结算", 
        '1' => "已结算"
    );
    
	// 初始化
	public function _initialize() {
		$this->setControllerTitle('');
		$this->setActionTitle(array(
				'index' => '结算列表',
		));
		$this->setActionNav(array(
				'index',
		));
		foreach( $this->status as $k => $v ) {
		    $this->setLinkNav($v, __CONTROLLER__."/index?status={$k}");
		}
	}
    
	public function getLists( $isDeleted = false ) {
	    $status = I('status', 0, 'intval');
	    $page = $this->getPage();
	    $pageSize = $this->getPageSize();
	    $agentName = I('agentName', "");
	    /*查询参数*/
	    $param = array();
	    $param['status'] = $status;
	    if ( !empty($agentName) ) {
	        $param['agentName'] = $agentName;
	    }
	    /*查询条件*/
	    $condition = array(
	        'a.status' => $status,
	        'a.cid' => $this->aid,
	    );
	    $param['agentName'] and $condition['c.name'] = $param['agentName'];
	    $d = M("AgentOrderSettle a")->join("__ORDER__ b on a.order_id=b.id", "LEFT")->join("__AGENT__ c on a.to_agent=c.id", "LEFT");
	    $settleList = $d->where($condition)->page($page)->limit($pageSize)->field("a.*,b.order_no,c.id as agent_id,c.name as agent_name")->select();
	    $count = $d->where($condition)->count();
	    $this->showPage($count, $pageSize);
	    $this->assign("params", $param);
	    $this->assign("settleList", $settleList);
	    $this->display();
	}
	
	/**
	 * 结算
	 */
	public function settleAccountsPostAction() {
	    $id = I("id", 0, "intval");
	    false !== M("AgentOrderSettle")->where(array("id"=>$id, "cid"=>$this->aid))
                    	    ->save(array(
                    	       'status' => 1,
                    	    )) and $this->ajaxSuccess("已设置为已结算");
	    $this->ajaxError("设置失败");
	}
}